﻿using System;


using System.Text;
using IAM.Interface.Model.Interface;
using System.Xml;
using System.Reflection;
using System.Collections;
using IAM.Interface.Model.ENUM;
using IAM.Interface.Model.Equals;
using System.Web;

namespace IAM.Interface.Model
{
    /// <summary>
    /// 纬度列表
    /// </summary>
    public class ACISessions : AbstractConvertObject,IConvertXml
    {
        #region 构造函数
        public ACISessions()
        {
            _acisessions = new System.Collections.ArrayList();
        }
        #endregion
        #region 属性
        
        private System.Collections.ArrayList _acisessions = null;
        private System.Collections.ArrayList _compareresultlist=null;
        /// <summary>
        /// ACSessions包含的权限描述信息的条数
        /// </summary>
        public int Count
        {
            get
            {
                return this.Items.Count;
            }
        }
        /// <summary>
        ///  纬度集合
        /// 【ACISession集合】
        /// </summary>
        public System.Collections.ArrayList Items
        {
            get
            {
                return _acisessions;
            }
            set
            {
                _acisessions = value;
            }

        }
        /// <summary>
        /// 比较结果集,包含CompareResult对象
        /// </summary>
        public System.Collections.ArrayList CompareresultList
        {
            get
            {
                return _compareresultlist;
            }
        }
        #endregion
        #region IConvertXml

        public string XMLBoday
        {
            get
            {
                StringBuilder _xml = new StringBuilder();
                _xml.Append("<ACISessions");
                _xml.Append(" Count=\"" + this.Count + "\"");
                _xml.Append(" >");
                foreach (object aciSession in this.Items)
                {
                    _xml.Append(((ACISession)aciSession).XML);
                }

                return _xml.ToString();
            }


        }

        public string XMLFinis
        {
            get
            {
                return "</ACISessions>";
            }
        }
        public string XML
        {
            get
            {
                return XMLBoday + XMLFinis;
            }
        }
        #endregion
        #region AbstractConvertObject
        public override object ConvertToObject()
        {
            foreach (XmlNode xn in this.CurrentXmlNode.ChildNodes)
            {
                ACISession acisession = new ACISession();
                acisession.CurrentXmlNode = xn;
                acisession = (ACISession)acisession.ConvertToObject();
                this.Items.Add(acisession);
            }
            return this;
        }
        #endregion
        #region 重写Equals
        /// <summary>
        /// 确定指定的 ACISessions 对象是否等效于当前 ACISessions
        /// 1.验证类型是否一致
        /// 2.验证集合数量是否一致
        /// 3.验证集合中对象是否一致【无顺比较】
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            //1.验证类型是否一致
            ACISessions acisessions = null;
            try
            {
                acisessions = (ACISessions)obj;
            }
            catch
            {
                return false;
            }
            //2.验证集合数量是否一致
            if (this.Items.Count != acisessions.Items.Count)
            {
                return false;
            }

            bool boolforeach = true;
            //3.验证集合中对象是否一致【无顺比较】
            foreach (object rudeobj in this.Items)
            {
                boolforeach = false;
                foreach (object aimobj in acisessions.Items)
                {
                    if (((ACISession)rudeobj).Equals(aimobj))
                    {
                        boolforeach = true;
                        break;
                    }
                }
                if (boolforeach == false)
                {
                    break;
                }
            }
            return boolforeach;

        }

        #endregion
        #region 重写GetHashCode
        /// <summary>
        /// 哈希函数
        /// 应为包含ACISession集合的顺序问题此函数不能作为对象的唯一标志
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            int result = 1;
            PropertyInfo[] pi = this.GetType().GetProperties();
            foreach (PropertyInfo onofpi in pi)
            {
                result = result + onofpi.GetHashCode();
            }
            return result;
        }
        #endregion
        #region 比较并记录对象的不同处
        /// <summary>
        /// 比较并记录对象的不同处
        /// ** CheckedItem的首行最好是唯一标识
        /// 1.将source xml化
        /// 2.将 obj xml化
        /// 3. 实例化CompareresultList
        /// 4. 遍历source的ACISession
        /// 4.1 寻找目标对象主键相同的 ACISession 节点
        /// 4.2 找不到目标项
        /// 4.3 比较 source 和 object 的ACISession 节点是否相同
        /// 5.雷同4
        /// </summary>
        /// <param name="source">源对象</param>
        /// <param name="obj">目标对象</param>
        public ArrayList GetCompareResultList(ACISessions source, ACISessions obj)
        {
            if (source == null)
            {
                source = new ACISessions();
            }
            if (obj == null)
            {
                obj = new ACISessions();
            }
            CompareResult cr = null;
            _compareresultlist = new ArrayList();
            //1.将source xml化
            XmlDocument xdsource = new XmlDocument();
            xdsource.LoadXml(source.XML);

            //2.将 obj xml化
            XmlDocument xdobj = new XmlDocument();
            xdobj.LoadXml(obj.XML);
            //3. 实例化CompareresultList
            _compareresultlist = new ArrayList();
            #region 4. 遍历source的ACISession
            //4. 遍历source的ACISession
            foreach (object oneofsourceitems in source.Items)
            {
                ACISession sourceACIS = (ACISession)oneofsourceitems;
                string key = "";//主键
                if (sourceACIS.Items.Count > 0)
                {
                    key = ((CheckedItem)sourceACIS.Items[0]).ACItemID;
                    key = IAM.Interface.Common.EnDeCoding.Encode(key);
                }
               
                //4.1 寻找目标对象主键相同的 ACISession 节点
                XmlNode xnobject = xdobj.SelectSingleNode("//CheckedItem[@ACItemID='"+key+"']");
                if (xnobject != null)
                {
                    xnobject = xnobject.ParentNode;
                }
                //4.2 找不到目标项
                if (xnobject == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = true;
                    cr.InObject = false;
                    cr.ContenctEquation = false;

                    this.CompareresultList.Add(cr);
                }
                else//找到目标项目
                {
                    //4.3 比较 source 和 object 的ACISession 节点是否相同
                    ACISession objectACIS = new ACISession();
                    objectACIS.CurrentXmlNode = xnobject;
                    objectACIS.ConvertToObject();

                    if (!sourceACIS.Equals(objectACIS))
                    {
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;

                        this.CompareresultList.Add(cr);
                    }
                }

            }
            #endregion
            #region 5.遍历object的ACISession
            //5.遍历object的ACISession
            foreach( object oneofobj in obj.Items)
            {
                

                ACISession objectACI = (ACISession)oneofobj;
                string key = "";//主键
                if (objectACI.Items.Count > 0)
                {
                    key = ((CheckedItem)objectACI.Items[0]).ACItemID;
                    key = IAM.Interface.Common.EnDeCoding.Encode(key);
                }

                //5.1 寻找源对象主键相同的 ACISession 节点
                XmlNode xnsource = xdsource.SelectSingleNode("//CheckedItem[@ACItemID='" + key + "']");
                if (xnsource != null)
                {
                    xnsource = xnsource.ParentNode;
                }
                //5.2 找不到目标项
                if (xnsource == null)
                {
                    cr = new CompareResult();
                    cr.ID = key;
                    cr.InSource = false;
                    cr.InObject = true;
                    cr.ContenctEquation = false;

                    this.CompareresultList.Add(cr);
                }
                else//找到源项目
                {
                    //5.3 比较 source 和 object 的ACISession 节点是否相同
                    ACISession sourACIS = new ACISession();
                    sourACIS.CurrentXmlNode = xnsource;
                    sourACIS.ConvertToObject();

                    if (!objectACI.Equals(sourACIS))
                    {
                        foreach (object objresult in this.CompareresultList)
                        {
                            if (((CompareResult)objresult).ID == key)
                            {
                                CompareresultList.Remove(objresult);
                                break;
                            }
                        }
                        cr = new CompareResult();
                        cr.ID = key;
                        cr.InSource = true;
                        cr.InObject = true;
                        cr.ContenctEquation = false;

                        this.CompareresultList.Add(cr);
                    }
                }
            }
            #endregion
            if (CompareresultList.Count > 0)
            {
                return CompareresultList;
            }
            else
            {
                return null;
            }
        }
        #endregion
    }
}
